﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Okuma.CLDATAPI.Enumerations;
using Okuma.CLDATAPI.DataAPI;
using OpenNETCF.MTConnect;
using System.Windows.Forms;

namespace Okuma
{
    public class Axis : ToolPart, IAxis
    {
        private CAxis m_axis;
        private ComponentType m_axisType;

        public AllAxisIndexEnum AxisIndex { get; set; }

        internal Axis(SubSystemEnum subSystem, AllAxisIndexEnum index, ILogService logService)
            : base(subSystem, logService)
        {

            m_axis = new CAxis(subSystem);

            AxisIndex = index;

            m_axis.SetSubSystem(subSystem);

            // set up our component type (Linear or Rotary)
            var type = m_axis.GetAxisType(index);
            if (type == AxisTypeEnum.Linear)
            {
                m_axisType = OpenNETCF.MTConnect.ComponentType.Linear;
            }
            else
            {
                m_axisType = OpenNETCF.MTConnect.ComponentType.Rotary;
            }

            Log.Log(LogPriority.Info, index.ToString() + "Axis loaded", "Okuma Adapter");

            Console.WriteLine("Load: " + Load);
            Console.WriteLine(string.Format("Command Pos: {0}", CommandedPosition));
            Console.WriteLine(string.Format("Encoder Pos: {0}", EncoderPosition));
            Console.WriteLine(string.Format("Machine Pos: {0}", MachinePosition));
            Console.WriteLine(string.Format("Program Pos: {0}", ProgramPosition));
        }

        public override ComponentType ComponentType
        {
            get { return m_axisType; }
        }

        [SampleDataItem(UnitValues.Millimiter,
            ItemType = DataItemType.POSITION, 
            ItemSubType = DataItemSubtype.ACTUAL
            )]
        // TODO: verify units
        public double CommandedPosition
        {
            get
            {
                try
                {
                    return m_axis.GetActualPositionCommandCoord((AxisIndex2Enum)AxisIndex);
                }
                catch(Exception ex)
                {
//                    Log.Log(ex, "CommandedPosition on " + this.AxisIndex.ToString());
                    return 0;
                }
            }
        }

        [SampleDataItem(UnitValues.Millimiter,
            ItemType = DataItemType.POSITION, 
            ItemSubType = DataItemSubtype.ACTUAL)]
        // TODO: verify units
        public double EncoderPosition
        {
            get
            {
                try
                {
                    return m_axis.GetActualPositionEncoderCoord((AxisIndex2Enum)AxisIndex);
                }
                catch(Exception ex)
                {
//                    Log.Log(ex, "EncoderPosition on " + this.AxisIndex.ToString());
                    return 0;
                }
            }
        }

        [SampleDataItem(UnitValues.Millimiter,
            ItemType = DataItemType.POSITION, 
            ItemSubType = DataItemSubtype.ACTUAL)]
        // TODO: verify units
        public double MachinePosition
        {
            get
            {
                try
                {
                    return m_axis.GetActualPositionMachineCoord((AxisIndex6Enum)AxisIndex);
                }
                catch
                {
                    return 0;
                }
            }
        }

        [SampleDataItem(UnitValues.Millimiter,
            ItemType = DataItemType.POSITION, 
            ItemSubType = DataItemSubtype.ACTUAL)]
        // TODO: verify units
        public double ProgramPosition
        {
            get
            {
                try
                {
                    return m_axis.GetActualPositionProgramCoord((AxisIndex1Enum)AxisIndex);
                }
                catch
                {
                    return 0;
                }
            }
        }

        [SampleDataItem(UnitValues.MillimiterPerSecond,
            ItemType = DataItemType.AXIS_FEEDRATE, 
            ItemSubType = DataItemSubtype.COMMANDED)]
        // TODO: verify units
        public double CommandFeedrate
        {
            get
            {
                try
                {
                    return m_axis.GetCommandFeedrate();
                }
                catch
                {
                    return 0;
                }
            }
        }

        [SampleDataItem(UnitValues.Percent,
            ItemType = DataItemType.LOAD)]
        // TODO: verify units
        public int Load
        {
            get
            {
                try
                {
                    return m_axis.GetAxisLoad((AxisIndex2Enum)AxisIndex);
                }
                catch
                {
                    return -1;
                }
            }
        }

        public override string Name
        {
            get { return m_axis.GetAxisName(AxisIndex); }
        }

    }
}
